/**
 * 文件上传
 */
/**
 *
 * 百度上传组件
 */

var Uploader = {
    target_alert: "",
    target_server: {
        url: "/basis/upload",
        type: "0",
    },
    server: {
        import: {
            url: "/basis/upload",
            type: '0'
        },
        goods_images: {
            url: "/fastdfs/upload",
            type: '1'
        },
        company_bm_images: {
            url: "/fastdfs/upload",
            type: '2'
        },
        company_am_images: {
            url: "/fastdfs/upload",
            type: '3'
        },
        company_cm_images: {
            url: "/fastdfs/upload",
            type: '4'
        }
    },
    layer_index:"",
    initUploader: function (server) {
        this.target_server = server;
        // 初始化Web Uploader
        var uploader = WebUploader.create({
            // 选完文件后，是否自动上传。
            auto: true,
            // swf文件路径
            swf: 'http://cdn.staticfile.org/webuploader/0.1.5/Uploader.swf',
            // 文件接收服务端。
            server: this.target_server.url,
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: '#filePicker',
            fileSingleSizeLimit: 10000000,
            // 允许上传文件类型。
            accept: {
                title: 'File',
                extensions: "pdf,doc,docx,jpg,jpeg,png,xls,xlsx",
                mimeTypes: "image/jpg,image/jpeg,image/png,application/pdf,application/msword," +
                "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
        });
        this.initUpload(uploader);
    },
    initUpload: function (uploader) {
        // 当有文件添加进来的时候
        uploader.on('fileQueued', function (file) {
            // console.log("fileQueued   : ", file);
            //更新上传是附加参数
            // console.log($("userId").val());
            uploader.option('formData', {
                userId: $("#userId").val(),
                goodsId: $("#current_goodsId").val(),
                fileType: $("#fileType").val()
            });
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on('uploadProgress', function (file, percentage) {
            console.log("uploadProgress   : ", file);
            var $li = $('#' + file.id),
                $percent = $li.find('.progress span');

            // 避免重复创建
            if (!$percent.length) {
                $percent = $('<p class="progress"><span></span></p>')
                    .appendTo($li)
                    .find('span');
            }
            $percent.css('width', percentage * 100 + '%')
            Uploader.layer_index = layer.load(0, {time: 60*1000});
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on('uploadSuccess', function (file, response) {
            console.log("uploadSuccess response  : ", response);
            layer.close( Uploader.layer_index);
            Uploader.success_callback(response);
        });

        // 文件上传失败，显示上传出错。
        uploader.on('uploadError', function (file) {
            console.log("uploadError  : ", file);
            layer.close( Uploader.layer_index);
            // layer.alert("导入数据出现错误，请稍后重试", {icon: 2, title: '文件上传提示'});
            alerts(Uploader.target_alert, 3, "导入数据出现错误，请稍后重试");
            /*   var $li = $('#' + file.id),
             $error = $li.find('div.error');
             // 避免重复创建
             if (!$error.length) {
             $error = $('<div class="error"></div>').appendTo($li);
             }
             $error.text('上传失败');*/
        });
        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on('uploadComplete', function (file) {
            layer.close( Uploader.layer_index);
            console.log("uploadComplete  : ", file);
            $('#' + file.id).find('.progress').remove();
            $("#fileSelected").text('');
            uploader.removeFile(file);
        });
        uploader.on('filesQueued', function (files) {
            var firstObj = $('#filePicker').children(':first'),
                selectText = '已选择' + (files && files.length ? files.length : 0) + '个文件';
            if (firstObj && firstObj[0].innerHTML.indexOf('已选择') != -1) {
                firstObj.html(selectText);
            } else {
                firstObj.before('<span id="fileSelected" class="file-selected">' + selectText + '个文件</span>');
            }
        });
    },
    success_callback: function (response) {
        // console.log("uploadSuccess response  : ", response);
        // console.log("this.target_server.type  : ", this.target_server.type);
        if (this.target_server.type == '1') {
            if (response.result == 'success') {
                Images.iteratorImagelist(response.content, Images.parent_target);
            }

        }
        if (this.target_server.type == '2') {
            if (response.result == 'success') {
                $("input[name='businesslicManagerId']").val(response.content[0].id);
                $("input[name='businesslicManager_url']").val(response.content[0].aliyunUrl);
            }

        }
        if (this.target_server.type == '3') {
            if (response.result == 'success') {
                $("input[name='authorizationManagerId']").val(response.content[0].id);
                $("input[name='authorizationManager_url']").val(response.content[0].aliyunUrl);
            }
        }
        if (this.target_server.type == '4') {
            if (response.result == 'success') {
                $("input[name='cardManagerId']").val(response.content[0].id);
                $("input[name='cardManager_url']").val(response.content[0].aliyunUrl);
            }
        }
        if (response.result == 'fail') {
            alerts(Uploader.target_alert, 3, response.errorMessage);
        }
    }
}
